package com.beaven.imclient.adapter;

import android.content.Context;
import android.support.annotation.LayoutRes;
import android.view.View;
import android.widget.TextView;
import com.beaven.imclient.R;
import com.beaven.imclient.utils.ImageUtil;
import com.beaven.model.data.helper.MessageHelper;
import com.beaven.model.data.helper.UserHelper;
import com.beaven.model.model.db.Message;
import com.beaven.model.model.db.Session;
import com.beaven.model.model.db.User;
import com.beaven.model.utils.DateTimeUtil;
import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import de.hdodenhof.circleimageview.CircleImageView;

/**
 * author: Beaven
 * time: 2017/6/1 15:27
 *
 * 会话列表Adapter
 */

public class SessionAdapter extends BaseQuickAdapter<Session, BaseViewHolder> {

  private Context context;

  public SessionAdapter(@LayoutRes int layoutResId, Context context) {
    super(layoutResId);
    this.context = context;
  }

  @Override protected void convert(BaseViewHolder helper, Session item) {
    CircleImageView imageView = helper.getView(R.id.image_account_session);
    // 会话标题
    helper.setText(R.id.text_session_title, item.getTitle());
    // 会话更新时间
    helper.setText(R.id.text_session_date, DateTimeUtil.getDateStatus(item.getModifyAt()));
    if (item.getReceiverType() == Message.RECEIVER_TYPE_NONE) {
      Message msg = MessageHelper.findLastWithUser(item.getId());
      // 最新的会话消息
      helper.setText(R.id.text_session_content, msg.getContent());
      User user = UserHelper.findFromLocal(item.getId());
      // 会话图标显示
      ImageUtil.show(Glide.with(context), imageView, user.getAccount());
    } else if (item.getReceiverType() == Message.RECEIVER_TYPE_GROUP) {
      Message msg = MessageHelper.findLastWithGroup(item.getId());
      helper.setText(R.id.text_session_content, msg.getContent());
      imageView.setImageResource(R.drawable.account);
    }
    // 未读消息数量标记
    int readCount = item.getUnReadCount();
    TextView textView = helper.getView(R.id.text_session_num);
    textView.setVisibility(readCount > 0 ? View.VISIBLE : View.GONE);
    textView.setText(String.valueOf(readCount));
  }
}
